import pymysql


#  数据库配置
host = 'localhost'
user = 'root'
password = 'root'
db = 'python_test'
charset = 'utf8'
port = 3306
autocommit = True  # 如果插入数据是否自动提交？和conn.commit()功能一致


def get_connection():
    conn = pymysql.connect(
        host=host,
        user=user,
        password=password,
        db=db,
        charset=charset,
        port=port,
        autocommit=autocommit
    )
    return conn


# 得到游标
def get_cursor(conn):
    return conn.cursor()


# 查询结果集 params为执行时所需的参数
def query(sql, params):
    conn = get_connection()
    cursor = get_cursor(conn)
    cursor.execute(sql, params)
    return cursor


# 查询结果集
def query(sql):
    conn = get_connection()
    cursor = get_cursor(conn)
    cursor.execute(sql)
    return cursor


# 执行更新SQL,包含:Insert、Delete、Update; params为执行时所需的参数
def update(sql, params):
    conn = get_connection()
    cursor = get_cursor(conn)
    cursor.execute(sql, params)
    return cursor

# 执行更新SQL,包含:Insert、Delete、Update
def update(sql):
    conn = get_connection()
    cursor = get_cursor(conn)
    cursor.execute(sql)
    return cursor


